package com.tuliyou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tuliyou.common.util.Result;
import com.tuliyou.dto.PageInfo;
import com.tuliyou.dto.UniMajorEnrollmentDTO;
import com.tuliyou.entity.Major;
import com.tuliyou.entity.UniMajorEnrollment;
import com.tuliyou.entity.University;
import com.tuliyou.entity.User;
import com.tuliyou.mapper.AdminMapper;
import com.tuliyou.request.UpdateUserRequest;
import com.tuliyou.service.AdminService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Override
    public Result<PageInfo<User>> getUsers(Integer page, Integer size, String username, String userType, Integer isDeleted) {
        // 设置分页参数
        PageHelper.startPage(page, size);
        // 动态SQL条件查询用户数据
        List<User> userList = adminMapper.getUsers(username, userType, isDeleted);
        // 把userList转换为Page类型
        Page<User> userPage = (Page<User>) userList;
        // 把数据封装到PageInfo
        PageInfo<User> pageInfo = new PageInfo<>();
        pageInfo.setCurrent(userPage.getPageNum()); // 当前页码
        pageInfo.setSize(userPage.getPageSize()); // 每页条数
        pageInfo.setTotal(userPage.getTotal()); // 总记录数
        pageInfo.setPages(userPage.getPages()); // 总页数
        pageInfo.setRecords(userList); // 当前页DTO列表
        // 数据封装到result并返回
        return Result.success(pageInfo);
    }

    @Override
    public Result<User> updateUser(Integer id, UpdateUserRequest updateUserRequest) {
        if(updateUserRequest.getUserType() == null && updateUserRequest.getIsDeleted() == null){
            return Result.error("修改信息为空");
        }
        int row = adminMapper.updateUser(id, updateUserRequest);
        if (row == 0){
            return Result.error("用户信息修改失败，请重试");
        }
        User user = adminMapper.getById(id);
        return Result.success(user);
    }





    @Override
    public Result<PageInfo<University>> getUniversities(Integer page, Integer size, String name,
                                                        Integer uniYear, String province, String city,
                                                        String level, String attribute, Integer isDeleted) {
        // 设置分页参数
        PageHelper.startPage(page, size);
        // 执行查询
        List<University> universityList = adminMapper.getUniversities(name, uniYear, province, city, level, attribute, isDeleted);
        // 关键：先将查询结果强转为Page<University>，保留分页信息
        Page<University> universityPage = (Page<University>) universityList;
        // 封装PageInfo对象
        PageInfo<University> pageInfo = new PageInfo<>();
        pageInfo.setCurrent(universityPage.getPageNum()); // 当前页码
        pageInfo.setSize(universityPage.getPageSize()); // 每页条数
        pageInfo.setTotal(universityPage.getTotal()); // 总记录数
        pageInfo.setPages(universityPage.getPages()); // 总页数
        pageInfo.setRecords(universityList); // 当前页列表
        return Result.success(pageInfo);
    }

    @Override
    public Result<University> insertUniversity(University university) {
        try {
            int row = adminMapper.insertUniversity(university);
            if (row == 0){
                return Result.error("院校新增失败,请重试");
            }
        } catch (Exception e) {
            log.info("院校新增异常{}",e.getMessage());
            return Result.error("院校新增异常");
        }
        return Result.success(university);
    }

    @Override
    public Result<University> updateUniversity(String universityCode, Integer uniYear, University university) {
        try {
            int row = adminMapper.updateUniversity(universityCode, uniYear, university);
            if (row == 0){
                return Result.error("院校修改失败,请重试");
            }
        } catch (Exception e) {
            log.info("院校修改异常{}",e.getMessage());
            return Result.error("院校修改异常");
        }
        return Result.success(university);
    }





    @Override
    public Result<PageInfo<Major>> getMajors(Integer page, Integer size, String majorCode, String majorName, String parentCode, Integer level, String degreeType, String years, Integer isDeleted) {
        // 设置分页参数
        PageHelper.startPage(page, size);
        // 执行查询
        List<Major> majorList = adminMapper.getMajors(majorCode,majorName,parentCode,level,degreeType,years,isDeleted);
        // 关键：先将查询结果强转为Page<University>，保留分页信息
        Page<Major> majorPage = (Page<Major>) majorList;
        // 封装PageInfo对象
        PageInfo<Major> pageInfo = new PageInfo<>();
        pageInfo.setCurrent(majorPage.getPageNum()); // 当前页码
        pageInfo.setSize(majorPage.getPageSize()); // 每页条数
        pageInfo.setTotal(majorPage.getTotal()); // 总记录数
        pageInfo.setPages(majorPage.getPages()); // 总页数
        pageInfo.setRecords(majorList); // 当前页列表
        return Result.success(pageInfo);
    }

    @Override
    public Result<Major> insertMajor(Major major) {
        try {
            int row = adminMapper.insertMajor(major);
            if (row == 0){
                return Result.error("专业新增失败,请重试");
            }
        } catch (Exception e) {
            log.info("专业新增异常{}",e.getMessage());
            return Result.error("专业新增异常");
        }
        return Result.success(major);
    }

    @Override
    public Result<Major> updateMajor(String majorCode, Major major) {
        try {
            int row = adminMapper.updateMajor(majorCode, major);
            if (row == 0){
                return Result.error("专业修改失败,请重试");
            }
        } catch (Exception e) {
            log.info("专业修改异常{}",e.getMessage());
            return Result.error("专业修改异常");
        }
        return Result.success(major);
    }





    @Override
    public Result<PageInfo<UniMajorEnrollment>> getUniMajorEnrollments(Integer page, Integer size, String universityCode, String majorName, Integer enrollYear, String province, String examType, String educationLevel, Integer isDeleted) {
        // 设置分页参数
        PageHelper.startPage(page, size);
        // 执行查询
        List<UniMajorEnrollment> uniMajorEnrollmentList = adminMapper.getUniMajorEnrollments(universityCode,majorName,enrollYear,province,examType,educationLevel,isDeleted);
        // 关键：先将查询结果强转为Page<University>，保留分页信息
        Page<UniMajorEnrollment> uniMajorEnrollmentPage = (Page<UniMajorEnrollment>) uniMajorEnrollmentList;
        // 封装PageInfo对象
        PageInfo<UniMajorEnrollment> pageInfo = new PageInfo<>();
        pageInfo.setCurrent(uniMajorEnrollmentPage.getPageNum()); // 当前页码
        pageInfo.setSize(uniMajorEnrollmentPage.getPageSize()); // 每页条数
        pageInfo.setTotal(uniMajorEnrollmentPage.getTotal()); // 总记录数
        pageInfo.setPages(uniMajorEnrollmentPage.getPages()); // 总页数
        pageInfo.setRecords(uniMajorEnrollmentList); // 当前页列表
        return Result.success(pageInfo);
    }

    @Override
    public Result<UniMajorEnrollment> insertUniMajorEnrollment(UniMajorEnrollmentDTO uniMajorEnrollmentDTO) {
        try {
            int row = adminMapper.insertUniMajorEnrollment(uniMajorEnrollmentDTO);
            if (row == 0){
                return Result.error("院校招生数据新增失败,请重试");
            }
        } catch (Exception e) {
            log.info("院校招生数据新增异常{}",e.getMessage());
            return Result.error("院校招生数据新增异常,请检查对应年份的院校代码是否存在");
        }
        UniMajorEnrollment ume = adminMapper.getNewUniMajorEnrollment();
        return Result.success(ume);
    }

    @Override
    public Result<UniMajorEnrollment> updateUniMajorEnrollment(Integer id, UniMajorEnrollmentDTO uniMajorEnrollmentDTO) {
        try {
            int row = adminMapper.updateUniMajorEnrollment(id, uniMajorEnrollmentDTO);
            if (row == 0){
                return Result.error("院校招生数据修改失败,请重试");
            }
        } catch (Exception e) {
            log.info("院校招生数据修改异常{}",e.getMessage());
            return Result.error("院校招生数据修改异常");
        }
        UniMajorEnrollment ume = adminMapper.getUniMajorEnrollmentById(id);
        return Result.success(ume);
    }
}
